package com.ldc.commonlibs.uts

import android.content.Context
import android.content.SharedPreferences


class DataStoreUts {
    companion object {
        private const val SP_NAME = "SP_NAME"
        private const val IS_LOGON = "isLogin"
        private const val COOKIE = "cookie"
        private const val USER_NAME = "user_name"
        private const val USER_PASS_WORD = "user_pass_word"
        val INSTANCE = SingleTon.S
    }

    private var editor: SharedPreferences.Editor? = null
    private var sharedPreferences: SharedPreferences? = null
    fun initSDK(context: Context) {
        sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE)
        editor = sharedPreferences?.edit()
    }

    private object SingleTon {
        val S = DataStoreUts()
    }

    fun setCookie(cookie: String) {
        editor?.putString(COOKIE, cookie)?.apply()
    }

    fun getCookie(): String {
        return "${sharedPreferences?.getString(COOKIE, "")}"
    }

    fun setIsLogin(isLogin: Boolean) {
        editor?.putBoolean(IS_LOGON, isLogin)?.apply()
    }

    fun getIsLogin(): Boolean {
        return sharedPreferences?.getBoolean(IS_LOGON, false) ?: false
    }

    fun setUserName(s: String) {
        editor?.putString(USER_NAME, s)?.apply()
    }

    fun getUserName() = sharedPreferences?.getString(USER_NAME, "")

    fun setUserPassWord(s: String) {
        editor?.putString(USER_PASS_WORD, s)?.apply()
    }

    fun getUserPassWord() = sharedPreferences?.getString(USER_PASS_WORD, "")
}